package music_store.ms.controller.portal;

import music_store.ms.common.Const;
import music_store.ms.common.ResponseCode;
import music_store.ms.common.ServerResponse;
import music_store.ms.pojo.User;
import music_store.ms.service.IMusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/music")
public class MusicController {

    @Autowired
    private IMusicService iMusicService;

    //查看歌曲信息。--游客也可用
    @PostMapping("detail.do")
    public ServerResponse musicDetail (Integer musicId){
        return iMusicService.getMusicDetailById(musicId);
    }

    //搜索歌曲。--游客也可用
    @PostMapping("search_music_by_musicname.do")
    public ServerResponse musicSearchByMusicName(String musicName,
                                      @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return iMusicService.searchMusicByMusicName(musicName,pageNum,pageSize);
    }

    @PostMapping("search_music_by_artist.do")
    public ServerResponse musicSearchByArtist(String artist,
                                      @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return iMusicService.searchMusicByArtist(artist,pageNum,pageSize);
    }

    @PostMapping("search_music_by_albumname.do")
    public ServerResponse musicSearchByAlbumName(String albumName,
                                              @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                              @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return iMusicService.searchMusicByAlbumName(albumName,pageNum,pageSize);
    }


    //以下操作接口需登录

    //检查登录状态
    private ServerResponse checkLoginStatus(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return ServerResponse.createBySuccess();
    }

    //收藏喜欢的歌曲
    @PostMapping("add_likes_music.do")
    public ServerResponse addLikesMusic(HttpSession session,Integer musicId){
        ServerResponse checkLoginStatus = checkLoginStatus(session);
        if (!checkLoginStatus.isSuccess()){
            return checkLoginStatus;
        }
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iMusicService.addLikesMusic(musicId,user.getId());
    }

























}
